package com.jianjian.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jianjian.dao.CheckGroupDao;
import com.jianjian.entity.PageResult;
import com.jianjian.entity.QueryPageBean;
import com.jianjian.pojo.CheckGroup;
import com.jianjian.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zsir
 * @create 2022-02-08 16:34
 */
 @Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组，同时需要让检查组关联检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {

        //新增检查组，操作t_checkgroup表
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多的关联关系，操作t_checkgroup_checkitem表
        Integer checkGroupId = checkGroup.getId();
       this.setCheckGroupAndCheckItem(checkGroupId,checkItemIds);

    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && queryString.length()>0){
            PageHelper.startPage(1,pageSize);
        }else {
            PageHelper.startPage(currentPage,pageSize);
        }

        Page<CheckGroup> page = checkGroupDao.findCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

//根据id查询检查组
    @Override
    public CheckGroup findById(Integer id) {
        return   checkGroupDao.findById(id);

    }
//根据检查组id查询检查组包含多个检查项id
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {

        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //编辑检查组，并修改检查项
    @Override
    public void edit(CheckGroup checkGroup, Integer[] id) {

        //修改检查组基本信息 ，操作检查组t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //清理当前检查组关联的检查项，操作中间关系表t_checkgroup_checkitem表
         checkGroupDao.deleteAssoication(checkGroup.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer checkGroupId = checkGroup.getId();
       this.setCheckGroupAndCheckItem(checkGroupId,id);

    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list =checkGroupDao.findAll();
        return list;
    }

    //建立检查组和检查项多对多关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkItemIds){
        if (checkItemIds !=null &&checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
